/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.application.web.conference;

import com.nus.application.ejb.conference.ConferenceSubmitEjbLocal;
import com.nus.application.ejb.user.ConferenceEjbLocal;
import com.nus.application.ejb.user.UserEjbLocal;
import com.nus.application.web.base.BaseAction;
import com.nus.application.web.conference.form.ConferenceSubmitForm;
import com.nus.constant.ConstantAuthorType;
import com.nus.dataaccess.dao.author.Author;
import com.nus.dataaccess.dao.conference.Conference;
import com.nus.dataaccess.dao.user.UserInfo;
import com.nus.dataaccess.scalar.FileUploadScalar;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ValidationAware;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;

/**
 *
 * @author robin.foe
 */
public class ConferenceSubmitMaintenance extends BaseAction implements ValidationAware , Preparable{
    
    @Inject
    ConferenceSubmitEjbLocal conferenceSubmitEjb;
    
    @Inject
    ConferenceEjbLocal conferenceEjb;
    
    @Inject
    UserEjbLocal userEjb;
    
     public String doList(){
        this.form.setFullListSize(conferenceSubmitEjb.getTotalCount(null));
        this.form.setPageNumber(super.getPage());
        System.err.println(this.getForm().getPageNumber());
        this.getForm().setConferences(
                conferenceSubmitEjb.paginate(form.calculateStartRow(super.getPage()), form.getObjectsPerPage(), null, false, null)
                );
        return SUCCESS;
    }
     
    public String doMaintain(){
        com.nus.dataaccess.dao.conference.ConferenceAuthor conferenceAuthor = conferenceSubmitEjb.getConferenceAuthor(
                            this.form.getConferenceAuthor().getConferenceId(), super.getCurrentLoginUser().getId());
        
        Conference conference = conferenceEjb.retrieveConferenceById(this.form.getConferenceAuthor().getConferenceId());
        
        if(conferenceAuthor == null){
            this.form.setAllowEdit(true);
            conferenceAuthor = new com.nus.dataaccess.dao.conference.ConferenceAuthor();
            conferenceAuthor.setConferenceId(this.form.getConferenceAuthor().getConferenceId());
            conferenceAuthor.setConference(conference);
            
            List<Author> authors = new ArrayList<Author>();
            Author author = new Author();
            author.setType(ConstantAuthorType.UPLOADER);
            author.setUserAccountId(super.getCurrentLoginUser().getId());
            authors.add(author);
            conferenceAuthor.setAuthors(authors);
            
            this.form.addFileUpload(new FileUploadScalar());
        }else{
            for(Author author : conferenceAuthor.getAuthors()){
                if(author.getUserAccountId().equals(super.getCurrentLoginUser().getId())){
                    if(author.isUploader())
                        this.form.setAllowEdit(true);
                    
                }
            }
        }
        
        this.form.setConferenceAuthor(conferenceAuthor);
        return SUCCESS;
    }
    
    public String doSave(){
        conferenceSubmitEjb.persistConferenceAuthor(this.form.getConferenceAuthor(), this.getForm().getFileUploads());
        super.addActionMessage("Information successfully saved");
        return SUCCESS;
    }
    
    public String doRemoveFile(){
        conferenceSubmitEjb.removeConferenceItem(new Long(this.form.getSelectedIndex()));
        super.addActionMessage("File successfully removed");
        return SUCCESS;
    }
    
    @Override
    public void validate(){

        if(!super.isSaveEvent()){
            if(super.isListEvent())
                super.clearActionErrors();
            
            return;
        }
        
        
        List<Long> userIds = new ArrayList<Long>();
        for(Author author : this.getForm().getConferenceAuthor().getAuthors()){
            if( userIds.contains(author.getUserAccountId()) ){
                super.addActionError("Author already exist for this conference. Please select other Author");
                break;
            }
            userIds.add(author.getUserAccountId());
        }
        
        
        
    }
    
    private List<UserInfo> participants = new ArrayList<UserInfo>();
    public List<UserInfo> getParticipants() {return participants;}
    public void setParticipants(List<UserInfo> participants) {this.participants = participants;}
    
    @Override
    public void prepare(){
        participants = userEjb.listAllUser();
    }
    
    private ConferenceSubmitForm form = new ConferenceSubmitForm();
    public ConferenceSubmitForm getForm() {return form;}
    public void setForm(ConferenceSubmitForm form) {this.form = form;}
    
    
}
